/**
 * @file   LoanAprvLoadDB.java
 * @date   2012-12-18上午11:43:52
 * @author liqing
 */
package com.gzceb.zhgjj.fileload.impl;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.gzceb.zhgjj.filecheck.FileCheckConstants;
import com.gzceb.zhgjj.filecheck.impl.LoanAprvCheck;
import com.gzceb.zhgjj.fileload.AbstractFileLoad;
import com.gzceb.zhgjj.mina.MinaConstants;
import com.gzceb.zhgjj.service.LoanAprvService;
import com.gzceb.zhgjj.util.SpringContextUtil;
import com.gzceb.zhgjj.util.Utils;
import com.gzceb.zhgjj.web.entity.LoanAprv;

/**
 * <p>贷款审批文件加载进数据库类。</p>
 *
 * @date   2012-12-18上午11:43:52 
 * @author liqing
 * @description  审批文件的响应文件读取后，加载进数据库表   info_loanaprv
 */
public class LoanAprvLoadDB extends AbstractFileLoad {

	private static Log logger = LogFactory.getLog(LoanAprvLoadDB.class);
	
	private LoanAprv loanAprv;
	private String absFileName; // 响应文件的绝对路径
	private List<String> fileList;  // 筛选的审批记录中需要存库的记录
	
	public LoanAprvLoadDB(String absFileName){
		this.absFileName = absFileName;
		fileList = new ArrayList<String>();
	}

	
	public LoanAprv getLoanAprv() {
		return loanAprv;
	}

	public void setLoanAprv(LoanAprv loanAprv) {
		this.loanAprv = loanAprv;
	}
	
	
	/* (non-Javadoc)
	 * @see com.gzceb.zhgjj.fileload.AbstractFileLoad#run()
	 */
	@Override
	public void run() {
		
		readFile(absFileName);
		saveToDB();
	}

	/* (non-Javadoc)
	 * @see com.gzceb.zhgjj.fileload.AbstractFileLoad#saveToDB()
	 */
	@Override
	public void saveToDB() {
		
		LoanAprvService las = (LoanAprvService) SpringContextUtil.getBean("loanAprvService");
		boolean rst = false;
		if ( 0 == fileList.size()){
			logger.info("the rsp file has no record to DB: " + absFileName);
		}else{
			// 从List中开始读取每一行的审批记录，循环记入库表
			for(int i=0; i< fileList.size(); i++){
				
				loanAprv.setContcode(fileList.get(i).substring(0, 32));
				loanAprv.setCustname(fileList.get(i).substring(32, 72).trim());
				loanAprv.setPaperkind(fileList.get(i).substring(72, 74));
				loanAprv.setPaperid(fileList.get(i).substring(74, 92));
				loanAprv.setWorkcorp(fileList.get(i).substring(92, 132));
				loanAprv.setFamiinco(Double.parseDouble(fileList.get(i).substring(132, 144).trim()));
				loanAprv.setSname(fileList.get(i).substring(144, 184).trim());
				loanAprv.setScertype(fileList.get(i).substring(184, 186).trim());
				loanAprv.setScertnum(fileList.get(i).substring(186, 204).trim());
				loanAprv.setSworkcorp(fileList.get(i).substring(204, 244).trim());
				loanAprv.setApplycapi(Double.parseDouble(fileList.get(i).substring(244, 256).trim()));
				loanAprv.setApplyterm(Integer.parseInt(fileList.get(i).substring(256, 264).trim()));
				loanAprv.setRetutype(fileList.get(i).substring(264, 268).trim());
				loanAprv.setBorroweraddr(fileList.get(i).substring(268, 368).trim());
				loanAprv.setHouse(fileList.get(i).substring(368, 468).trim());
				loanAprv.setHousearea(Double.parseDouble(fileList.get(i).substring(468, 478).trim()));
				loanAprv.setHouseamt(Double.parseDouble(fileList.get(i).substring(478, 490).trim()));
				loanAprv.setSelfinance(Double.parseDouble(fileList.get(i).substring(490, 502).trim()));
				loanAprv.setSelfprop(Double.parseDouble(fileList.get(i).substring(502, 512).trim()));
				loanAprv.setLoanforpurchase(Double.parseDouble(fileList.get(i).substring(512, 522).trim()));
				loanAprv.setMonthlydepositsum(Double.parseDouble(fileList.get(i).substring(522, 534).trim()));
				loanAprv.setAssuagent(fileList.get(i).substring(534, 634).trim());
				loanAprv.setAssukind(fileList.get(i).substring(634, 638).trim());
				loanAprv.setAvgmonthlyrepay(Double.parseDouble(fileList.get(i).substring(638, 650).trim()));
				loanAprv.setAuditamt(Double.parseDouble(fileList.get(i).substring(650, 662).trim()));
				loanAprv.setAuditterm(Integer.parseInt(fileList.get(i).substring(662, 670).trim()));
				loanAprv.setAuditrate(Double.parseDouble(fileList.get(i).substring(670, 680).trim()));
				loanAprv.setAprvamt(Double.parseDouble(fileList.get(i).substring(680, 692).trim()));
				loanAprv.setAprvterm(Integer.parseInt(fileList.get(i).substring(692, 700).trim()));
				loanAprv.setAprvrate(Double.parseDouble(fileList.get(i).substring(700, 710).trim()));
				loanAprv.setAssignees(fileList.get(i).substring(710, 750).trim());
				loanAprv.setReceiptdate(fileList.get(i).substring(750, 758).trim());
				loanAprv.setAuditor(fileList.get(i).substring(758, 798).trim());
				loanAprv.setAuditdate(fileList.get(i).substring(798, 838).trim());
				loanAprv.setAprvor(fileList.get(i).substring(838, 878).trim());
				loanAprv.setAprvdate(fileList.get(i).substring(878, 886).trim());
				loanAprv.setHandbankname(fileList.get(i).substring(886, 966).trim());
				loanAprv.setAcctmgr(""); // 默认客户经理ID为空
				loanAprv.setCurroptor("");  // 默认当前处理人为空
				loanAprv.setAuthorstat("0"); // 默认赋值授权状态为 0：未授权 
				loanAprv.setLoanstat("00"); 
				loanAprv.setInptime(Utils.getCurrDateStr19Str());
				
				rst = las.saveToDB(loanAprv);
				if(false == rst){
					logger.error("failed to save record to DB. Record : " + fileList.get(i));
				}
			}
			
		}

	}


	@Override
	public void readFile(String absFileName) {
		// 将响应文件的内容去除不需存库的记录，生成List
		BufferedReader br = null;
		logger.info("begin to read rsp file: " + absFileName);
		try{
			br = new BufferedReader(new FileReader(absFileName));
			String tmpStr = null;
			boolean first = false;
			while( (tmpStr = br.readLine()) != null){
				if( false == first ){
					// 第一行信息是总记录，跳过
					first = true;
					continue;
				}
				// 响应文件中每一行的末尾最后一个字节如果为9，表示接受，需要存库，记录到 List中
				if( tmpStr.endsWith(FileCheckConstants.RT_ACCEPT.getValue()) ){
					fileList.add(tmpStr);
				}
			}
		}catch(Exception e){
			logger.error("error happened when read rsp file: " + absFileName);
		}finally{
			if (br != null) {
				try {
					br.close();
				} catch (Exception ec) {
					logger.error("Errors happened when closinig BufferedReader.\n" + ec);
				}
			}
		}
		
		logger.info("the rsp file has been read to List. The list size is : " + fileList.size());
	}
	
	
	
}
